# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql

class NewhousePipeline(object):
    def __init__(self):
        self.db = pymysql.connect("192.168.0.179", "root", "123456", "NewHouseDB")
        self.cursor = self.db.cursor()

    def process_item(self, item, spider):
        sql, data = item.get_sql()
        try:
            self.cursor.execute(sql, data)
            self.db.commit()
        except:
            # print(sql, data, '-' * 100)
            self.db.rollback()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.db.close()
